﻿package com.tudou.home.newskin.buttons
{
    import com.tudou.home.common.events.*;
    import com.tudou.skin.*;
    import com.tudou.skin.common.bar.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;

    public class ProgressBar extends BaseBar implements IProgress, ITransform, ISkipHeadTail
    {
        private var playedBar:DisplayObject;
        private var loadedBar:DisplayObject;
        private var skipMarkTail:DisplayObject;
        private var skipMarkHead:DisplayObject;
        private var totalBar:DisplayObject;
        private var drag:DisplayObject;
        private var skipTail:Number = 0;
        private var skipHead:Number = 0;

        public function ProgressBar()
        {
            totalBar = new TotalBarMC();
            loadedBar = new LoadedBarMC();
            playedBar = new PlayedBarMC();
            drag = new PlayDragerMC();
            skipMarkHead = new SkipMarkMC();
            skipMarkTail = new SkipMarkMC();
            drag.y = int(totalBar.height / 2);
            addChild(totalBar);
            addChild(loadedBar);
            addChild(playedBar);
            addChild(skipMarkHead);
            addChild(skipMarkTail);
            addChild(drag);
            initSkipMarks();
            return;
        }// end function

        override public function getValue() : Number
        {
            return getValueByX(drag.x);
        }// end function

        private function skipMarkX() : void
        {
            skipMarkTail.x = totalWidth * skipTail - Math.ceil(skipMarkTail.width / 2);
            skipMarkHead.x = totalWidth * skipHead;
            return;
        }// end function

        public function setStarted(param1:Number) : void
        {
            return;
        }// end function

        private function skipMarkOutHlr(event:MouseEvent) : void
        {
            return;
        }// end function

        public function setSkipHeadTailValue(param1:Number, param2:Number) : void
        {
            skipHead = param1;
            skipTail = param2;
            if (skipHead > 0)
            {
                skipMarkHead.visible = true;
            }
            if (skipTail > 0)
            {
                skipMarkTail.visible = true;
            }
            skipMarkX();
            return;
        }// end function

        private function skipMarkOverHlr(event:MouseEvent) : void
        {
            if (skipMarkHead == event.currentTarget)
            {
                dispatchEvent(new SkipEvent(SkipEvent.HEAD));
            }
            if (skipMarkTail == event.currentTarget)
            {
                dispatchEvent(new SkipEvent(SkipEvent.TAIL));
            }
            return;
        }// end function

        public function setPlayed(param1:Number) : void
        {
            setValue(param1);
            return;
        }// end function

        override public function getActMin() : Number
        {
            return int(drag.width / 2);
        }// end function

        public function setLoaded(param1:Number) : void
        {
            loadedBar.width = totalWidth * param1;
            return;
        }// end function

        private function get totalWidth() : Number
        {
            return totalBar.width;
        }// end function

        public function getValueByX(param1:Number) : Number
        {
            var _loc_2:* = getActMin();
            var _loc_3:* = getActMax();
            return MathUtil.unit((param1 - _loc_2) / (_loc_3 - _loc_2));
        }// end function

        public function getPlayHead() : Number
        {
            return getValue();
        }// end function

        override public function getMin() : Number
        {
            return 0;
        }// end function

        override public function getMax() : Number
        {
            return totalWidth;
        }// end function

        public function hasSkipHeadTail(param1:Boolean) : void
        {
            return;
        }// end function

        override public function setValue(param1:Number) : void
        {
            var _loc_2:* = getActMin();
            var _loc_3:* = getActMax();
            drag.x = _loc_2 + param1 * (_loc_3 - _loc_2);
            playedBar.width = totalWidth * param1;
            return;
        }// end function

        override public function getActMax() : Number
        {
            return totalWidth - int(drag.width / 2);
        }// end function

        private function initSkipMarks() : void
        {
            skipMarkHead.y = -2;
            skipMarkTail.y = -2;
            skipMarkHead.addEventListener(MouseEvent.MOUSE_OVER, skipMarkOverHlr);
            skipMarkTail.addEventListener(MouseEvent.MOUSE_OVER, skipMarkOverHlr);
            skipMarkHead.visible = false;
            skipMarkTail.visible = false;
            return;
        }// end function

        public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            var _loc_4:* = loadedBar.width / totalWidth;
            var _loc_5:* = getValue();
            totalBar.width = param1;
            setLoaded(_loc_4);
            setPlayed(_loc_5);
            skipMarkX();
            return;
        }// end function

    }
}
